package com.smbms.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.smbms.entity.User;
import com.smbms.tool.Constants;

//自定义拦截器类需要继承HandlerInterceptorAdapter
public class LoginInterceptor extends HandlerInterceptorAdapter{
	//重写preHandle()方法
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		User user = (User)request.getSession().getAttribute(Constants.USER_SESSION);	//获取session
		//判断页面跳转时session是否有用户
		if(user == null){
			//如果没有,则进行拦截
			response.sendRedirect(request.getContextPath() + "/401.jsp");	//重定向到401页面
			return false;
		}else{
			return true;
		}
	}
}
